<?php

namespace view;

class Chart {

    /**
     *
     * @var \Imagick
     */
    private $image;
    private $width;
    private $height;

    public function __construct($width, $maxValue, $data, $month, $year, $title) {
        $this->width = $width;
        $this->height = $this->width / 2;
        $this->image = new \Imagick();
        $this->image->newImage($width, $this->height, new \ImagickPixel("white"));
        $draw = new \ImagickDraw();

        $draw->setStrokeColor(new \ImagickPixel("black"));

        //grid
        $draw->setStrokeWidth(1);
        $number = 8;
        $draw->setStrokeColor(new \ImagickPixel("grey"));
        for ($i = 1; $i <= sizeof($data); $i++) {
            $draw->line(0.15 * $this->width + (0.85 - 0.15) / sizeof($data) * $i * $this->width, 0.15 * $this->height, 0.15 * $this->width + (0.85 - 0.15) / sizeof($data) * $i * $this->width, 0.84 * $this->height);
        }
        for ($i = 0; $i < $number; $i++) {
            $draw->line(0.15 * $this->width, 0.15 * $this->height + (0.85 - 0.15) / $number * $i * $this->height, 0.85 * $this->width, 0.15 * $this->height + (0.85 - 0.15) / $number * $i * $this->height);
        }
        $draw->setStrokeWidth(1);
        $draw->settextkerning(6);
        $draw->setfontfamily("Verdana");
        $draw->setStrokeColor(new \ImagickPixel("red"));
        $draw->setFontSize(21);
        $draw->annotation(0.25 * $this->width, 0.03 * $this->height, $month . " " . $year . " - " . $title);
        $draw->settextkerning(2);
        $draw->setFontSize(14);
        $draw->annotation(0.87 * $this->width, 0.93 * $this->height, "[ dzień ]");
        $draw->annotation(0.01 * $this->width, 0.10 * $this->height, "[ " . $title . " ]");
        $draw->settextkerning(1);
        for ($i = 0; $i < sizeof($data); $i = $i + 2) {
            $draw->annotation(0.14 * $this->width + (0.85 - 0.15) / sizeof($data) * $i * $this->width, 0.93 * $this->height, $i + 1);
            //$draw->line(0.15*$this->width, 0.85*$this->height, 0.85*$this->width, 0.85*$this->height);
            //$draw->line(0.15*$this->width+(0.85-0.15)/sizeof($data)*$i*$this->width, 0.15*$this->height, 0.15*$this->width+(0.85-0.15)/sizeof($data)*$i*$this->width, 0.84*$this->height);
        }
        for ($i = 0; $i <= $number; $i++) {
            $draw->annotation(0.07 * $this->width, 0.87 * $this->height - (0.85 - 0.15) / $number * $i * $this->height, round($maxValue / $number * $i, 1));
            //$draw->annotation(250, 75, "Lorem Ipsum!");
            //$draw->line(0.15*$this->width, 0.15*$this->height+(0.85-0.15)/$number*$i*$this->height, 0.85*$this->width, 0.15*$this->height+(0.85-0.15)/$number*$i*$this->height);
        }
        $draw->setStrokeWidth(3);
        $draw->setStrokeColor(new \ImagickPixel("black"));
        $draw->line(0.15 * $this->width, 0.15 * $this->height, 0.15 * $this->width, 0.85 * $this->height);
        $draw->line(0.15 * $this->width, 0.85 * $this->height, 0.85 * $this->width, 0.85 * $this->height);
        $draw->line(0.13 * $this->width, 0.25 * $this->height, 0.15 * $this->width, 0.15 * $this->height);
        $draw->line(0.17 * $this->width, 0.25 * $this->height, 0.15 * $this->width, 0.15 * $this->height);
        $draw->line(0.78 * $this->width, 0.81 * $this->height, 0.85 * $this->width, 0.85 * $this->height);
        $draw->line(0.78 * $this->width, 0.89 * $this->height, 0.85 * $this->width, 0.85 * $this->height);
        // $draw->line(350, 170, 100, 150);

        $draw->setStrokeWidth(1);
        $draw->setFillColor(new \ImagickPixel("red"));
        $draw->setStrokeOpacity(1);
        $draw->setFontSize(11);
        for ($i = 0; $i < sizeof($data); $i++) {
            if ($maxValue > 0) {
                $draw->rectangle(0.15 * $this->width + (0.85 - 0.15) / sizeof($data) * $i * $this->width
                        , 0.85 * $this->height - (0.85 - 0.15) / $maxValue * $data[$i + 1] * $this->height
                        , 0.15 * $this->width + (0.85 - 0.15) / sizeof($data) * $i * $this->width + (0.85 - 0.15) / sizeof($data) * $this->width
                        , 0.85 * $this->height);
                if ($data[$i + 1] > 0) {
                    $draw->annotation(0.155 * $this->width + (0.85 - 0.15) / sizeof($data) * $i * $this->width, 0.82 * $this->height - (0.85 - 0.15) / $maxValue * $data[$i + 1] * $this->height, $data[$i + 1]);
                }
            }
            else {
                $draw->annotation(0.155 * $this->width + (0.85 - 0.15) / sizeof($data) * $i * $this->width, 0.82 * $this->height , 0);
            }
        }

        $this->image->setImageFormat("png");
        $this->image->drawImage($draw);
    }

    public function __toString() {
        return "<img src='data:image/jpg;base64," . base64_encode($this->image->getImageBlob()) . "' />";
    }

}
